<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class goods_uqcms extends control
{
    function __construct()
    {
        $this->spid = $_SESSION['shop']['spid'];
        parent::__construct();
    }
    public function ajax_get_next_list()
    {
        $pcid = isset($_POST['pcid']) ? $_POST['pcid'] : '0';
        $query = $this->db->get_all("select cid,pcid,name from " . table('category') . " where pcid = " . $pcid . " and status = 1");
        if ($query) {
            foreach ($query as $k => $v) {
                $clist[] = $v;
            }
            if (isset($clist)) {
                $data['error'] = '0';
                $data['last'] = '0';
                $data['category'] = $clist;
                echo json_encode($data);
            } else {
                $data['error'] = '0';
                $data['last'] = '1';
                echo json_encode($data);
            }
        } else {
            $data['error'] = '0';
            $data['last'] = '1';
            echo json_encode($data);
        }
    }
    public function ajax_get_parents_list()
    {
        $cid = isset($_POST['cid']) ? $_POST['cid'] : error_json('提交错误');
        $cats_arr = $this->module('category')->get_parents_cid($cid);
        $cats_list = $this->module('category')->get_parents_list($cats_arr);
        if ($cats_list) {
            echo json_encode($cats_list);
        } else {
            error_json('获取失败');
        }
    }
    public function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '10';
        $key = isset($_GET['key']) ? $_GET['key'] : '';
        $wsql = '';
        if ($key) {
            $wsql .= " title like '%" . $key . "%' and ";
        }
        $status = isset($_GET['status']) ? $_GET['status'] : '1';
        if ($status) {
            $wsql .= " status = 1 and ";
        } else {
            $wsql .= " status = 0 and ";
        }
        if (!empty($wsql)) {
            $wsql = ' where shop_id = ' . $this->spid . ' and ' . rtrim($wsql, ' and ');
        } else {
            $wsql = ' where shop_id = ' . $this->spid;
        }
        $result = $this->db->get_all("select * from " . table('goods') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $v) {
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('goods') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        $cid = isset($_GET['cid']) ? $_GET['cid'] : '0';
        if ($id) {
            $row = $this->db->get_one("select * from " . table('goods') . " where shop_id = " . $this->spid . " and id = " . $id);
            if ($row['id']) {
                $cid = !empty($cid) ? $_GET['cid'] : $row['cid'];
                $row['content'] = $this->module('album')->img_content($row['content']);
                $attr_result = $this->db->get_all("select * from " . table('goods_attr_index') . " where gid = " . $id);
                if ($attr_result) {
                    foreach ($attr_result as $k => $v) {
                        $attr_data[] = $v['attr_id'];
                    }
                    $this->assign('selected_attr', $attr_data);
                }
                $goods_img = $this->db->get_all("select * from " . table('goods_img') . " where gid = " . $id);
                $this->assign('img_data', $goods_img);
                $sku_all = $this->db->get_all("select * from " . table('goods_sku_index') . " where gid = " . $id);
                if ($sku_all) {
                    if (count($sku_all) == '1' && $sku_all[0]['value'] == '0') {
                        $row['code'] = $sku_all[0]['code'];
                    }
                    foreach ($sku_all as $val) {
                        $sku_arr[$val['value']] = $val;
                        $value_sku[] = $val['value'];
                    }
                    $value_skus = array_unique($value_sku);
                    $sku_in = implode(',', $value_skus);
                    $sku_in_arr = explode(',', $sku_in);
                    $this->assign("checked_sku", array_unique($sku_in_arr));
                    $sku_img_arr = $this->db->get_all("select * from " . table('goods_sku_img') . " where gid = " . $id);
                    if (isset($sku_img_arr)) {
                        $goods_sku_images = $this->module('goods_sku')->get_color_images($sku_in, $sku_img_arr);
                        $this->assign('goods_sku_images', $goods_sku_images);
                    }
                    $this->assign('goods_sku_list', $this->module('goods_sku')->get_list_func($sku_in, $sku_arr));
                } else {
                    $value_skus[] = array();
                    $this->assign("checked_sku", $value_skus);
                }
                $this->assign('row', $row);
            }
        }
        $this->assign('cats', $this->module('category')->get_parents_name($cid));
        $cid_in = $this->module('category')->get_parents_cid($cid);
        if ($cid_in) {
            $cid_in = implode(',', $cid_in);
            $this->assign('brand', $this->module('goods')->get_brand($cid_in));
            $this->assign('attr', $this->module('category_attr')->get_attr($cid_in));
            $this->assign('sku', $this->module('goods_sku')->sku_checkbox($cid_in));
            $this->assign('express', $this->module('express')->get_tpl($this->spid));
        }
        $this->assign('goods_group', $this->goods_group());
        $this->display('goods/edit');
    }
    public function save()
    {
        if ($_POST) {
            $this->db->autocommit();
            $id = isset($_POST['id']) ? $_POST['id'] : false;
            if ($id) {
                $row = $this->db->get_one("select id,shop_id from " . table('goods') . " where shop_id = " . $this->spid . " and id = " . $id);
                if ($row && !$row['id']) {
                    error_json('提交的产品不存在');
                }
            }
            $data['title'] = P('title', '标题不能为空');
            $data['intro'] = P('intro');
            $cid = P('cid', '分类不能为空');
            $data['cid'] = $cid;
            $data['group_cid'] = P('group_cid', '', '0');
            $data['shop_id'] = $this->spid;
            $data['brand'] = P('brand', '', '0');
            $data['images'] = trim($_POST['goods_img'][0]) ? $_POST['goods_img'][0] : error_json('图片不能为空！');
            $content = isset($_POST['content']) ? $_POST['content'] : '';
            $data['content'] = $this->module('album')->img_replace($content);
            $data['price'] = !empty($_POST['price']) ? $_POST['price'] : error_json('价格不能为空');
            $data['market_price'] = !empty($_POST['market_price']) ? $_POST['market_price'] : error_json('市场价格不能为空');
            $data['express'] = P('express', '', '0');
            $data['stock'] = P('stock', '', '0');
            $data['status'] = P('status', '', '1');
            $data['addtime'] = time();
            if ($id) {
                $query = $this->db->update(table('goods'), $data, 'id = ' . $id);
            } else {
                $query = $this->db->add(table('goods'), $data);
                $id = $this->db->insert_id();
            }
            $this->db->delete(table('promote_seckill'), 'gid = ' . $id);
            $this->db->delete(table('promote_seckill_sku'), 'gid = ' . $id);
            $img_arr = isset($_POST['goods_img']) ? $_POST['goods_img'] : error_json('最少需要传一张图片');
            $goods_img_px = isset($_POST['goods_img_px']) ? $_POST['goods_img_px'] : '';
            $query_gimg = $this->db->delete(table('goods_img'), 'gid = ' . $id);
            for ($i = 0; $i < count($img_arr); $i++) {
                $dataImg['gid'] = $id;
                $dataImg['url'] = $img_arr[$i];
                $dataImg['px'] = $goods_img_px[$i];
                $dataImg['addtime'] = time();
                $this->db->add(table('goods_img'), $dataImg);
            }
            $query_attr = $this->db->delete(table('goods_attr_index'), 'gid = ' . $id);
            if (isset($_POST['attr'])) {
                $attr_arr = $_POST['attr'];
                $attr_num = count($attr_arr);
                for ($i = 0; $i < $attr_num; $i++) {
                    $attrsql['gid'] = $id;
                    $attrsql['gcid'] = $cid;
                    $attrsql['attr_id'] = $attr_arr[$i];
                    $this->db->add(table('goods_attr_index'), $attrsql);
                }
            } else {
                $attrsql['gid'] = $id;
                $attrsql['gcid'] = $cid;
                $attrsql['attr_id'] = '';
                $this->db->add(table('goods_attr_index'), $attrsql);
            }
            $this->db->delete(table('goods_sku_index'), 'gid = ' . $id);
            $this->db->delete(table('goods_sku_img'), 'gid = ' . $id);
            $sku_val = isset($_POST['sku_val']) ? $_POST['sku_val'] : false;
            if ($sku_val) {
                $sku_price = isset($_POST['sku_price']) ? $_POST['sku_price'] : '';
                $sku_stock = isset($_POST['sku_stock']) ? $_POST['sku_stock'] : '';
                $sku_code = isset($_POST['sku_code']) ? $_POST['sku_code'] : '';
                $sku_img_id = isset($_POST['sku_img_id']) ? $_POST['sku_img_id'] : '';
                $sku_img_value = isset($_POST['sku_img_value']) ? $_POST['sku_img_value'] : '';
                $sku_img_name = isset($_POST['sku_img_name']) ? $_POST['sku_img_name'] : '';
                if ($sku_img_id) {
                    $sku_img_num = count($sku_img_id);
                    for ($i = 0; $i < $sku_img_num; $i++) {
                        $sku_img['gid'] = $id;
                        $sku_img['sku_id'] = $sku_img_id[$i];
                        $sku_img['images'] = $sku_img_value[$i];
                        $sku_img['name'] = $sku_img_name[$i];
                        $this->db->add(table('goods_sku_img'), $sku_img);
                    }
                }
                $sku_name_arr = $this->sku_post_name($sku_val, $sku_img_id, $sku_img_name);
                $sku_num = count($sku_val);
                for ($i = 0; $i < $sku_num; $i++) {
                    $sku_data['gid'] = $id;
                    $sku_data['value'] = $sku_val[$i];
                    $arrr = explode(',', $sku_val[$i]);
                    foreach ($arrr as $v) {
                        $name_arr[] = $sku_name_arr[$v];
                    }
                    $sku_data['name'] = serialize($name_arr);
                    unset($name_arr);
                    $sku_data['price'] = $sku_price[$i];
                    $sku_data['stock'] = $sku_stock[$i];
                    $sku_data['code'] = $sku_code[$i];
                    $this->db->add(table('goods_sku_index'), $sku_data);
                }
                $this->db->update(table('goods'), array('sku_num' => $sku_num), 'id = ' . $id);
            } else {
                $sku_data['gid'] = $id;
                $sku_data['value'] = '0';
                $sku_data['name'] = '';
                $sku_data['price'] = P('price');
                $sku_data['stock'] = P('stock');
                $sku_data['code'] = P('code');
                $this->db->add(table('goods_sku_index'), $sku_data);
                $this->db->update(table('goods'), array('sku_num' => '0'), 'id = ' . $id);
            }
            if ($query) {
                $this->db->commit();
                right_json();
            } else {
                $this->db->rollback();
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    function sku_post_name($sku_val_arr, $sku_img_id_arr = null, $sku_img_name_arr = null)
    {
        $sku_val_in = implode(',', $sku_val_arr);
        $sku_all = $this->db->get_all("select * from " . table('goods_sku') . " where id in (" . $sku_val_in . ")");
        if ($sku_all) {
            foreach ($sku_all as $v) {
                $sku_pid_arr[] = $v['pid'];
            }
            $sku_pid_arr_in = implode(',', array_unique($sku_pid_arr));
            $psku_all = $this->db->get_all("select * from " . table('goods_sku') . " where id in (" . $sku_pid_arr_in . ")");
            if ($psku_all) {
                foreach ($psku_all as $v) {
                    $parr[$v['id']] = $v['name'];
                }
            }
            if (!empty($sku_img_id_arr)) {
                foreach ($sku_img_id_arr as $k => $v) {
                    $sku_img_arr[$v] = $sku_img_name_arr[$k];
                }
            }
            foreach ($sku_all as $v) {
                if (isset($sku_img_arr[$v['id']]) && !empty($sku_img_arr[$v['id']])) {
                    $sku_arrs[$v['id']] = array($parr[$v['pid']], $sku_img_arr[$v['id']]);
                } else {
                    $sku_arrs[$v['id']] = array($parr[$v['pid']], $v['name']);
                }
            }
            return $sku_arrs;
        }
    }
    public function sku_img()
    {
        $goods_id = isset($_POST['goods_id']) ? $_POST['goods_id'] : false;
        $val = isset($_POST['val']) ? $_POST['val'] : '';
        if ($goods_id) {
            $sku_img_arr = $this->db->get_all("select * from " . table('goods_sku_img') . " where gid = " . $goods_id);
            if (isset($sku_img_arr)) {
                echo $this->module('goods_sku')->get_color_images($val, $sku_img_arr);
            }
        } else {
            echo $this->module('goods_sku')->get_color_images($val);
        }
    }
    public function sku_list()
    {
        $goods_id = isset($_POST['goods_id']) ? $_POST['goods_id'] : '';
        $val = !empty($_POST['val']) ? $_POST['val'] : '';
        $val = rtrim($val, ',');
        if ($goods_id) {
            $sku_all = $this->db->get_all("select * from " . table('goods_sku_index') . " where gid = " . $goods_id);
            if ($sku_all) {
                foreach ($sku_all as $key => $v) {
                    $sku_arr[$v['value']] = $v;
                }
                echo $this->module('goods_sku')->get_list_func($val, $sku_arr);
            }
        } else {
            echo $this->module('goods_sku')->get_list_func($val, '');
        }
    }
    public function del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('提交错误');
        $sql = "select g.oid,g.shop_id,g.gid,i.id,i.uid,i.shop_id,i.order_status from " . table('order_goods') . " as g right join " . table('order_info') . " as i on g.oid = i.id where i.order_status in (1,2,3,4) and g.gid = " . $id . " limit 1";
        $row = $this->db->get_one($sql);
        if ($row['id']) {
            showmsg('该商品正在交易中，无法删除');
        } else {
            $this->db->autocommit();
            $goods_query = $this->db->delete(table('goods'), 'shop_id = ' . $this->spid . ' and id = ' . $id);
            $img_query = $this->db->delete(table('goods_img'), 'gid =' . $id);
            $del1 = $this->db->delete(table('goods_attr_index'), 'gid =' . $id);
            $del2 = $this->db->delete(table('distribute_goods'), 'gid =' . $id);
            $del3 = $this->db->delete(table('promote_seckill'), 'gid =' . $id);
            $del4 = $this->db->delete(table('promote_seckill_sku'), 'gid =' . $id);
            if ($goods_query && $img_query && $del1 && $del2 && $del3 && $del4) {
                $this->db->commit();
                showmsg('删除成功！');
            } else {
                $this->db->rollback();
                showmsg('删除失败');
            }
        }
    }
    function goods_group()
    {
        return $this->db->get_all("select * from " . table('goods_group') . " where shop_id = " . $this->spid);
    }
    public function group()
    {
        $data = $this->db->get_all("select * from " . table('goods_group') . " where shop_id = " . $this->spid . " order by px desc");
        $this->assign('data', $data);
        $this->display();
    }
    public function group_edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('goods_group') . " where shop_id = " . $this->spid . " and id = " . $id);
            if ($row['id']) {
                $this->assign('row', $row);
            }
        }
        $this->display();
    }
    public function group_save()
    {
        if ($_POST) {
            $setarr['name'] = P('name', '分组名称不能为空');
            $setarr['px'] = P('px', '', '0');
            $id = P('id', 0);
            if ($id) {
                $query = $this->db->update(table('goods_group'), $setarr, 'id = ' . $id . ' and shop_id = ' . $this->spid);
            } else {
                $setarr['shop_id'] = $this->spid;
                $setarr['addtime'] = time();
                $query = $this->db->add(table('goods_group'), $setarr);
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        }
    }
    public function group_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('提交错误');
        $query = $this->db->delete(table('goods_group'), 'id = ' . $id . ' and shop_id = ' . $this->spid);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
}